package com.bw.order.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.bw.common.utils.R;
import com.bw.order.config.IdWorker;
import com.bw.order.entity.HousePurchaseOrderEntity;
import com.bw.order.feign.HouseFeign;
import com.bw.order.to.HouseNewroomTo;
import com.bw.order.to.HouseRentTo;
import com.bw.order.vo.HouseUserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bw.common.utils.PageUtils;
import com.bw.common.utils.Query;

import com.bw.order.dao.HouseRentOrderDao;
import com.bw.order.entity.HouseRentOrderEntity;
import com.bw.order.service.HouseRentOrderService;


@Service("houseRentOrderService")
public class HouseRentOrderServiceImpl extends ServiceImpl<HouseRentOrderDao, HouseRentOrderEntity> implements HouseRentOrderService {
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private HouseFeign houseFeign;
    @Autowired
    private IdWorker idWorker;
    @Autowired
    private HouseRentOrderDao houseRentOrderDao;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<HouseRentOrderEntity> page = this.page(
                new Query<HouseRentOrderEntity>().getPage(params),
                new QueryWrapper<HouseRentOrderEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public R add(Map<String, Object> params) {
        //uuid查看redis数据是否失效
        String uuids = (String) params.get("uuids");
        //获取用户id查看是否登录或者失效
        String uid = (String) params.get("uid");
        //租房的期限 3、6、9、12、
        String renttime = (String) params.get("renttime");
        //获取用户登录信息
        String user = redisTemplate.opsForValue().get(uid);
        //获取订单编号
        String s = redisTemplate.opsForValue().get(uuids);
        //判断段是否登录或者登录过期
        if (s!=null&&!s.equals("")){
            HouseUserVo houseUserVo = JSON.parseObject(user, new TypeReference<HouseUserVo>() {
            });
            if (s!=null&&!s.equals("")){
                HouseRentTo house = houseFeign.findrent(s);
                HouseRentOrderEntity orderEntity = new HouseRentOrderEntity();
                if (house!=null){
                    long oid = idWorker.nextId();
                    //租房的订单id
                    orderEntity.setRentOid(oid);
                    //租房id
                    orderEntity.setRentId(house.getRentId());
                    //房东的手机号
                    orderEntity.setUserPhone(house.getHouUserPhone());
                    //房屋图片
                    orderEntity.setHouseImg(house.getHouseImg());
                    //租期
                    orderEntity.setLeaseTrem(renttime);
                    //租凭价格
                    orderEntity.setHousePrice(house.getHousePrice());
                    //创建时间
                    orderEntity.setCreateTime(new Date());
                    //订单价格
                    BigDecimal housePrice = house.getHousePrice();
                    double dd = housePrice.doubleValue() * Double.parseDouble(renttime);
                    orderEntity.setOrderPrice(BigDecimal.valueOf(dd));
                    //订单状态
                    orderEntity.setOrderStatus(0);
                    houseRentOrderDao.add(orderEntity);
                    return R.ok();
                }else {
                    System.out.println("信息错误");
                    return R.error(250,"不存在该房屋");
                }
            }else {
                System.out.println("该订单失效");
                return R.error(404,"111");
            }
        }else {
            System.out.println("登录失效或未登录");
            return R.error(403,"无发购买，重新登录");
        }
        //如果是空值可能是登录失效或者未登录
    }

}